package com.livew.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.livew.model.User;
import com.livew.service.UserService;
import com.livew.utils.AjaxResult;

@Controller
@RequestMapping("user")
public class UserController {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	private UserService userService;

	/**
	 * 保存用户
	 * 
	 * @param name
	 * @param sex
	 * @param age
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResult save(User user) {
		logger.debug(user + "");
		user = new User();
		user.setAge(10);
		user.setName("张三");
		user.setSex("男");
		userService.saveUser(user);
		return AjaxResult.getResult(Boolean.TRUE, user.getName());
	}

	/**
	 * 获取所有用户列表
	 * 
	 * @return
	 */
	@RequestMapping("/getall")
	@ResponseBody
	public Object getAllUser() {
		List<User> users = userService.getAllUser();
		for (User u : users) {
			logger.debug(u.toString());
		}
		return users;
	}

	/**
	 * 根据用户id获取用户信息
	 * 
	 * @return
	 */
	@RequestMapping("/getUserById/{id}")
	@ResponseBody
	public Object getUserById(@PathVariable("id")Integer id) {
		User user = userService.getUserById(id);
		logger.debug(user.toString());
		return user;
	}

	/**
	 * 根据用户id获取用户信息
	 * 
	 * @return
	 */
	@RequestMapping("/renameUser")
	@ResponseBody
	public Integer renameUser() {
		Integer id = 10;
		String name="wanglu";
		userService.renameUser(name, id);
		return 1;
	}

	/**
	 * 根据用户id获取用户信息
	 * 
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Integer delete(@RequestParam(value = "id", defaultValue = "0") String id) {
		logger.debug(id);
		userService.deleteUserById(Integer.parseInt(id));
		return 1;
	}
}
